//by k1 2021.7.3

if (!isNil "heliTrans_cancel") then {heliTrans_cancel = true};
if (!isNil "heliTrans_cancel") exitWith {systemchat "请稍候..."};
heliTrans_cancel = false;

if (isNil "Escape_extVeh_sel") exitWith {heliTrans_cancel = nil; systemchat "请先选择载具"};
if (!alive Escape_extVeh_sel) exitWith {heliTrans_cancel = nil; systemchat "载具已损毁"};

_heli = Escape_extVeh_sel;
_driver = driver _heli;
if (!alive _driver) then {{if (toLower(_x select 1) in ["turret","gunner","commander"] && {_x select 3 isEqualTo [0]} && {_x select 2 == -1}) exitWith {_driver = _x select 0}} forEach fullCrew _heli};
if (!alive _driver || {isPlayer _driver} || {!(_driver in _heli)}) exitWith {heliTrans_cancel = nil; systemchat "载具无AI驾驶员"};

_grp = group _driver;
if (_this == 2) exitWith {heliTrans_cancel = nil; while {count waypoints _grp > 0} do {deleteWaypoint ((waypoints _grp) select 0)}; systemchat "执行"; sleep 2; _wp = _grp addWaypoint [getPos _heli, 0]; _wp setWaypointType "MOVE"};

_t = time + 15;
if (!local _grp) then {[_grp, clientOwner] remoteExec ["setGroupOwner", 2]; systemchat "等待设置机组为本地"; waitUntil {local _grp || {time > _t}}; if(local _grp)then{systemchat "设置成功"}};
if (time > _t) exitWith {heliTrans_cancel = nil; systemchat "操作无效，无法设置机组为本地"};

if (!alive _driver || {isPlayer _driver} || {!(_driver in _heli)}) exitWith {heliTrans_cancel = nil; systemchat "载具无AI驾驶员"};

if (_this == 3) exitWith {heliTrans_cancel = nil; _grp leaveVehicle _heli; systemchat "机组离开载具"};

heliTrans_grp = _grp;
heliTrans_GetClick = false;
onMapSingleClick {
	_marker = heliTrans_grp getVariable ["heliTrans_mak", ""];
	if (_marker != "") then {deleteMarkerLocal _marker};
	_marker = createMarkerLocal [format["heliTrans_mak_%1_%2", heliTrans_grp,time/60], _pos];
	_marker setMarkerShapeLocal "ICON";
	_marker setMarkerColorLocal "ColorBlack";
	_marker setMarkerTypeLocal "mil_end";
	heliTrans_grp setVariable ["heliTrans_mak", _marker];
	heliTrans_GetClick = true;
};
systemchat "点击地图选择位置";
_t = time + 30;
waitUntil {heliTrans_GetClick || {heliTrans_cancel} || {time > _t}};
if (heliTrans_cancel) exitWith {heliTrans_cancel = nil; systemchat "操作取消"};
heliTrans_cancel = nil;
onMapSingleClick "";
if (time > _t) exitWith {systemchat "长时间未点击地图，操作取消"};

if (heliTrans_GetClick) then {
	_marker = _grp getVariable ["heliTrans_mak", ""];
	_pos = markerPos _marker;
	_heli setVariable ["heliTrans_Waypoint_complete", false];
	while {count waypoints _grp > 0} do {deleteWaypoint ((waypoints _grp) select 0)};
	_grp setVariable ["heliTrans_change", 1];
	sleep .1;
	_grp setVariable ["heliTrans_change", nil];
	_wp0 = _grp addWaypoint [getPos _heli, 0];
	_wp0 setWaypointType "MOVE";
	_wp0 setWaypointSpeed "FULL"; //"NORMAL"
	_wp0 setWaypointBehaviour "CARELESS";
	_wp0 setWaypointStatements ["true", (if(isNil "heliTrans_fly_h")then[{"_heli = vehicle this; _heli stop false; _heli enableAI 'MOVE'; _heli enableAI 'PATH';"},{format["vehicle this flyInHeight %1;", heliTrans_fly_h]}])]; //一直会使用设置后的飞行高度
	_wp1 = _grp addWaypoint [_pos, 0];
	_wp1 setWaypointType "MOVE";
	_grp setSpeedMode "FULL";
	_grp setBehaviour "CARELESS";
	if (_heli isKindOf "air") then {systemchat format["飞行高度: %1", if(isNil "heliTrans_fly_h")then[{"默认"},{heliTrans_fly_h}]]};
	heliTrans_fly_h = nil;
	
	_varName = Format ["heliTrans_%1_%2", _heli,time/60];
	uiNamespace setVariable [_varName, [_heli, _pos]];
	waitUntil {!isNull (findDisplay 12)};
	_DrawArrowMap = ((findDisplay 12) displayCtrl 51) ctrlAddEventHandler ["Draw", compile format [
		'
			(uiNamespace getVariable "%1") params ["_heli", "_pos"];
			if (alive _heli) then {(_this select 0) drawArrow [getPosASLVisual _heli,_pos,[0,0,0,.7]]};
		',
		_varName
	]];
	
	switch _this do {
		case 0: {
			_marker setMarkerTypeLocal "mil_pickup_noShadow";
			_marker setMarkerTextLocal "载具前往这里";
			_wp1 setWaypointStatements ["true", "vehicle this setVariable ['heliTrans_Waypoint_complete', true];"]; //条件满足全局执行
		};
		case 1: {
			if (isNil "heliTrans_land_mode") then {heliTrans_land_mode = 'LAND'};
			_marker setMarkerTextLocal "直升机着陆区";
			_wp1 setWaypointStatements ["true", format["_heli = vehicle this; _heli setVariable ['heliTrans_Waypoint_complete', true]; _heli land '%1';", heliTrans_land_mode]];
			systemchat format["着陆方式: ""%1""", heliTrans_land_mode];
			heliTrans_land_mode = nil;
		};
	};
	
	waitUntil {_heli getVariable ["heliTrans_Waypoint_complete", true] || {!alive _driver} || {!alive _heli} || {!(_driver in _heli)} || {count waypoints _grp == 0}};
	if (count waypoints _grp > 0 && {_heli getVariable ["heliTrans_Waypoint_complete", true]}) then [{systemchat '到达指定位置'; if(_this == 1)then{_t = time + 25; waitUntil {!(_heli getVariable ["heliTrans_Waypoint_complete", true]) || {time > _t}}}},{systemchat '取消路径点'}];
	((findDisplay 12) displayCtrl 51) ctrlRemoveEventHandler ["Draw", _DrawArrowMap];
	uiNamespace setVariable [_varName, nil];
	deleteMarkerLocal _marker;
	if (isNil {_grp getVariable "heliTrans_change"}) then {
		while {count waypoints _grp > 0} do {deleteWaypoint ((waypoints _grp) select 0)};
		_grp setSpeedMode "NORMAL";
		_grp setBehaviour "AWARE";
		_grp setVariable ["heliTrans_mak", nil];
	};
};


